﻿namespace com.vs.General
{
    using com.vs.DataAccess;
    using System;
    using System.Globalization;
    using System.Runtime.InteropServices;
    using System.Text;

    public class DateManager
    {
        public static string ConvertDateIntoInterfaceDateTime(string date, string format)
        {
            return DateTime.ParseExact(date, new string[] { format, format + "HHmmss" }, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None).ToString("s", CultureInfo.InvariantCulture);
        }

        public static string ConvertDateTimeToLPFormat(DateTime dateTime)
        {
            if (dateTime == DateTime.MinValue)
            {
                return "";
            }
            return new StringBuilder().Append(StringManager.Pad(dateTime.Year, 4, '0')).Append(StringManager.Pad(dateTime.Month, 2, '0')).Append(StringManager.Pad(dateTime.Day, 2, '0')).Append(StringManager.Pad(dateTime.Hour, 2, '0')).Append(StringManager.Pad(dateTime.Minute, 2, '0')).Append(StringManager.Pad(dateTime.Second, 2, '0')).ToString();
        }

        public static DateTime ConvertProntoStringDateToSystemDateTime(string dateInStringFormat)
        {
            if (StringManager.IsEmpty(dateInStringFormat))
            {
                return DateTime.MinValue;
            }
            int year = int.Parse(dateInStringFormat.Substring(0, 4));
            int month = int.Parse(dateInStringFormat.Substring(4, 2));
            int day = int.Parse(dateInStringFormat.Substring(6, 2));
            int hour = int.Parse(dateInStringFormat.Substring(8, 2));
            int minute = int.Parse(dateInStringFormat.Substring(10, 2));
            return new DateTime(year, month, day, hour, minute, int.Parse(dateInStringFormat.Substring(12, 2)));
        }

        public static DateTime ConvertStringDateIntoDateTime(string date, string format)
        {
            return DateTime.ParseExact(date, new string[] { format }, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None);
        }

        public static string ConvertTimeSpanToFriendlyString(TimeSpan ts)
        {
            return string.Concat(new object[] { ts.Days, ":", ts.Hours, ":", ts.Minutes, ":", ts.Seconds });
        }

        public static DateTime ConvertToLocalTime(DateTime date)
        {
            return date;
        }

        private static string ConvertToSQLFormat(DateTime date)
        {
            if (date == DateTime.MinValue)
            {
                return "";
            }
            return SqlRetrieval.MaskStringLiteral(new StringBuilder().Append(date.Year).Append("-").Append(StringManager.Pad(date.Month.ToString(), 2, '0')).Append("-").Append(StringManager.Pad(date.Day.ToString(), 2, '0')).Append(" ").Append(StringManager.Pad(date.Hour.ToString(), 2, '0')).Append(":").Append(StringManager.Pad(date.Minute.ToString(), 2, '0')).Append(":").Append(StringManager.Pad(date.Second.ToString(), 2, '0')).ToString());
        }

        public static DateTime ConvertToUniversalTime(DateTime date)
        {
            return date;
        }

        public static string FormatDate(string dateTime, string outputFormat)
        {
            return DateTime.Parse(dateTime).ToString(outputFormat + "hhmmss");
        }

        public static string FormatDateTime(DateTime dateTime, string outputDateFormat)
        {
            return dateTime.ToString(outputDateFormat);
        }

        public static string GetFormatDateNoTimeSQL(string fieldName, string fieldNameAlias)
        {
            string str = " CONVERT(DateTime, FLOOR(CONVERT( Float, " + fieldName + "))) ";
            if (!StringManager.IsEmpty(fieldNameAlias))
            {
                str = str + "AS " + fieldNameAlias + " ";
            }
            return str;
        }

        public static DateTime GetMaxDateForDB()
        {
            return Convert.ToDateTime("12/31/4712", CultureInfo.InvariantCulture);
        }

        public static DateTime GetMinDateForDB()
        {
            return Convert.ToDateTime("1/1/1753", CultureInfo.InvariantCulture);
        }

        [Obsolete("Please use IsDateStringValid(string date, string format,out DateTime result)", false)]
        public static bool IsDateStringValid(string date, string format)
        {
            try
            {
                if (StringManager.Equals(format, "YYYYMMDD"))
                {
                    date = date + "000000";
                }
                ConvertProntoStringDateToSystemDateTime(date);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool IsDateStringValid(string date, string format, out DateTime result)
        {
            return DateTime.TryParseExact(date, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
        }

        public static bool IsEqual(DateTime dt1, DateTime dt2)
        {
            return (DateTime.Compare(dt1, dt2) == 0);
        }

        public static bool IsGreaterThan(DateTime dt1, DateTime dt2)
        {
            return (DateTime.Compare(dt1, dt2) > 0);
        }

        public static bool IsGreaterThanOrEqual(DateTime dt1, DateTime dt2)
        {
            return (DateTime.Compare(dt1, dt2) >= 0);
        }

        public static bool IsLeapYear(int year)
        {
            return (((year % 400) == 0) || (((year % 100) != 0) && ((year % 4) == 0)));
        }

        public static bool IsLessThan(DateTime dt1, DateTime dt2)
        {
            return (DateTime.Compare(dt1, dt2) < 0);
        }

        public static bool IsLessThanOrEqual(DateTime dt1, DateTime dt2)
        {
            return (DateTime.Compare(dt1, dt2) <= 0);
        }

        public static DateTime ParseWithInvariantCulture(string stDate)
        {
            return DateTime.Parse(stDate, CultureInfo.InvariantCulture);
        }

        public static string ToDateTimeStringWithInvariantCulture(DateTime datetime)
        {
            return datetime.ToString("G", CultureInfo.InvariantCulture);
        }

        public static string ToStringWithInvariantCulture(DateTime date)
        {
            return date.ToString("d", CultureInfo.InvariantCulture);
        }

        public static string ToStringWithInvariantCulture(string date)
        {
            return ToStringWithInvariantCulture(DateTime.Parse(date));
        }
    }
}

